Page({
  data: {
    schema:[
    ],
    formData:{
    },
    title: '',
    result: '',
    remark: '',
    apiName: '', 
    // 三个下拉框的展开状态（初始均收起）
    open1: false,
    open2: false,
    open3: false,
    // 三个下拉框的选中值（初始为空）
    selected1: null,
    selected2: null,
    selected3: null,
    // 三个下拉框的选项数据（可自定义）
    list2: ['console.log', 'CameraContext.onCameraFrame', 'CameraFrameListener.start', 'Image.src'],
    list1: ['openBluetoothAdapter','getSystemInfoSync.return.safeArea.left','getSystemInfo.success.screenWidth','showToast.object.image','onCompassChange.callback.direction','request.object.method.GET'],
    list3: ['live-player','text.selectable','button.open-type.contact'],bufferArray: [] ,// 用于存储字节数组
    param:""
  },

  onLoad(options) {
      wx.setNavigationBarTitle({
        title: options.title,
      })
      
      this.setData({
        title: options.title,
        zh:options.zh,
        remark: options.remark,
        webUrl: options.webUrl
      })
   
  },
  choose1(e){
    const fs = wx.getFileSystemManager()
    fs.writeFileSync(`${wx.env.USER_DATA_PATH}/hello.txt`,  'hello, world', 'utf8')
    this.setData({
      result: wx.env,
      apiName: e.currentTarget.dataset.apiname 
    })
  },
  /**
   * 切换下拉框状态：点击头部时触发
   * @param {*} e - 事件对象，通过 data-index 区分哪个下拉框
   */
  toggleDropdown(e) {
    const index = e.currentTarget.dataset.index; // 获取当前点击的下拉框索引（1/2/3）
    const currentOpen = `open${index}`; // 拼接当前状态的键名（如 open1）

    // 构建新状态：切换当前下拉框，关闭另外两个
    const newData = {};
    newData[currentOpen] = !this.data[currentOpen]; // 切换当前状态
    // 强制关闭另外两个
    for (let i = 1; i <= 3; i++) {
      if (i !== parseInt(index)) {
        newData[`open${i}`] = false;
      }
    }

    this.setData(newData); // 更新状态
  },

  /**
   * 选择下拉选项：点击列表项时触发
   * @param {*} e - 事件对象，包含当前下拉框索引和选中值
   */
  selectItem(e) {
    const index = e.currentTarget.dataset.index; // 下拉框索引（1/2/3）
    console.log(index)
    if(index==1){
      this.setData({
        selected2:'',
        selected3:''
      })
    }
    if(index==2){
      this.setData({
        selected1:'',
        selected3:''
      })
    }
    if(index==3){
      this.setData({
        selected2:'',
        selected1:''
      })
    }
    const value = e.currentTarget.dataset.value; // 选中的值
    const selectedKey = `selected${index}`; // 拼接选中值的键名（如 selected1）
    const openKey = `open${index}`; // 拼接状态键名（如 open1）

    // 更新选中值，并关闭当前下拉框
    this.setData({
      [selectedKey]: value,
      [openKey]: false
    });
  },
  parmsInput: function (e) {
    this.setData({
      param: e.detail.value
    })
  },
  choose2(e){
    if(this.data.param){
      var str =this.data.param.toString();
    if(wx.canIUse(str)){
      this.setData({
        result: str+'当前版本可用',
        apiName: e.currentTarget.dataset.apiname 
      })
    }else{
      this.setData({
        result: str+'当前版本不可用',
        apiName: e.currentTarget.dataset.apiname 
      })
    } 
  }else{
    wx.showToast({
      title: '请选择判断参数',
    })
  }
  },
  arrayBufferToHex(buffer) {
    return Array.from(new Uint8Array(buffer))
      .map(b => b.toString(16).padStart(2, '0'))
      .join(' ');
  },
  choose3(e){
    const base64 = 'ISAwIDIwMCAyMDAgMTAwOCAxClBBR0UtV0lEVEggNjA4CkdBUC1TRU5TRQpMSU5FIDI0IDEwMCA1NTIgMTAwIDEKTElORSAyNCAxNjQgNTUyIDE2NCAxCkxJTkUgMjQgMzA4IDU1MiAzMDggMQpMSU5FIDI0IDM4OCA0NTYgMzg4IDEKTElORSAyNCA2NzYgNDU2IDY3NiAxCkxJTkUgMjQgODIwIDU1MiA4MjAgMQpMSU5FIDcyIDM4OCA3MiA2NzYgMQpMRUZUClNFVE1BRyAxIDEKU0VUQk9MRCAwClQgNTUgMCA1NiA3MiAyMDI1LTEwLTIzIDE1OjQxOjExCkxFRlQKU0VUTUFHIDIgMgpTRVRCT0xEIDEKVCAyNCAwIDE0OCAxMDUgMzAwLSBGMTggMDQwClNFVEJPTEQgMApMRUZUCkxFRlQKU0VUTUFHIDIgMgpTRVRCT0xEIDEKVCAyNCAwIDI0IDMyNCC7ps73ClNFVEJPTEQgMApMRUZUClNFVE1BRyAxIDE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Lz+INPFu9224LbgClNFVEJPTEQgMApCIFFSIDI5IDY4NyBNIDIgVSAzCk1BLGh0dHBzOi8vcS56dG8uY29tL2MvaWR4P2NoYW5uZWw9eWR5CkVORFFSCkxFRlQKU0VUTUFHIDEgMQpTRVRCT0xEIDAKVlQgNTUgMCA1NTIgMzk2ICogNzM1NyA3MzcxIDU4MzQgNDUKTEVGVApMRUZUClNFVE1BRyAxIDEKU0VUQk9MRCAwClZUIDU1IDAgNTU1IDc5MSAqIDczNTcgNzM3MSA1ODM0IDQ1CkxFRlQKTEVGVApTRVRNQUcgMSAxClNFVEJPTEQgMApUMjcwIDU1IDAgMjMgNjQyICogNzM1NyA3MzcxIDU4MzQgNDUKTEVGVApMRUZUClNFVE1BRyAxIDEKU0VUQk9MRCAwClQyNzAgNTUgMCAyMyAyNTMgKiA3MzU3IDczNzEgNTgzNCA0NQpMRUZUCkxFRlQKU0VUTUFHIDEgMQpTRVRCT0xEIDAKVCA1NSAwIDM2OCAyNCAqIDczNTcgNzM3MSA1ODM0IDQ1CkxFRlQKTEVGVApTRVRNQUcgMSAxClNFVEJPTEQgMApUIDI0IDAgMjggODIzIMn6u+7Tw8a3KjEKRk9STQpQUklOVAo='
    const arrayBuffer = wx.base64ToArrayBuffer(base64)
    console.log(this.arrayBufferToHex(arrayBuffer))
    if(arrayBuffer){
      this.setData({
        result: this.arrayBufferToHex(arrayBuffer),
        apiName: e.currentTarget.dataset.apiname 
      })
    }else{
      this.setData({
        result: 'Base64转成ArrayBuffer对象失败',
        apiName: e.currentTarget.dataset.apiname 
      })
    } 
  },
  choose4(e){
    const arrayBuffer = new Uint8Array([11, 22, 33])
    const base64 = wx.arrayBufferToBase64(arrayBuffer)
    this.setData({
      result: base64,
      apiName: e.currentTarget.dataset.apiname 
    })
  },
})